{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.81481275 -7.01784255 -5.77153032  1.54721511]\n",
      " [-3.47636513  6.77149643  6.16426438  1.02702711]\n",
      " [-1.61067897 -3.52467375  3.0787187   1.1106886 ]]\n",
      "[[  4.6046822 ]\n",
      " [ 12.72060187]\n",
      " [ -9.32047038]\n",
      " [  2.85136158]]\n",
      "[[  1.66494761e-01   2.86183158e-02   9.56006250e-01   7.52257661e-01]\n",
      " [  6.13838799e-03   9.62558698e-01   9.99903220e-01   8.94515045e-01]\n",
      " [  7.69258997e-01   2.63896877e-05   6.33989368e-02   9.34496451e-01]\n",
      " [  9.34493069e-02   2.25102360e-02   9.69864425e-01   9.75515535e-01]]\n",
      "[[ 0.0035588 ]\n",
      " [ 0.99594444]\n",
      " [ 0.99637475]\n",
      " [ 0.00390575]]\n",
      "[[ -8.06431365e-06  -4.46272986e-06   4.94707582e-06  -6.70622661e-06]\n",
      " [  4.60168691e-07   7.50970871e-06  -1.47746478e-08   4.40725171e-06]\n",
      " [  1.07027166e-05   4.39569518e-09  -7.24722322e-06   2.28555681e-06]\n",
      " [ -5.92757801e-06  -4.25313804e-06   4.13940546e-06  -1.03487234e-06]]\n",
      "[[ -1.26200034e-05]\n",
      " [  1.63809008e-05]\n",
      " [  1.30947627e-05]\n",
      " [ -1.51953275e-05]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "X = np.array([ [0,0,1],[0,1,1],[1,0,1],[1,1,1] ])\n",
    "y = np.array([[0,1,1,0]]).T\n",
    "syn0 = 2*np.random.random((3,4)) - 1\n",
    "syn1 = 2*np.random.random((4,1)) - 1\n",
    "for j in range(60000):\n",
    "    l1 = 1/(1+np.exp(-(np.dot(X,syn0))))\n",
    "    l2 = 1/(1+np.exp(-(np.dot(l1,syn1))))\n",
    "    l2_delta = (y - l2)*(l2*(1-l2))\n",
    "    l1_delta = l2_delta.dot(syn1.T) * (l1 * (1-l1))\n",
    "    syn1 += l1.T.dot(l2_delta)\n",
    "    syn0 += X.T.dot(l1_delta)\n",
    "\n",
    "print(syn0)\n",
    "print(syn1)\n",
    "print(l1)\n",
    "print(l2)\n",
    "print(l1_delta)\n",
    "print(l2_delta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Output After Training:\n",
      "[[0 0 1]\n",
      " [0 1 1]\n",
      " [1 0 1]\n",
      " [1 1 1]]\n",
      "[[ 9.67299303]\n",
      " [-0.2078435 ]\n",
      " [-4.62963669]]\n",
      "[[ 0.44822538]\n",
      " [ 0.9999225 ]]\n",
      "[[-0.00966449]\n",
      " [-0.00786506]\n",
      " [ 0.00641102]\n",
      " [ 0.00788043]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    " \n",
    "# sigmoid function\n",
    "def nonlin(x,deriv=False):\n",
    "    if(deriv==True):\n",
    "        return x*(1-x)\n",
    "    return 1/(1+np.exp(-x))\n",
    " \n",
    "# input dataset\n",
    "X = np.array([  [0,0,1],\n",
    "                [0,1,1],\n",
    "                [1,0,1],\n",
    "                [1,1,1] ])\n",
    " \n",
    "# output dataset            \n",
    "y = np.array([[0,0,1,1]]).T\n",
    "\n",
    "# seed random numbers to make calculation\n",
    "# deterministic (just a good practice)\n",
    "np.random.seed(1)\n",
    "\n",
    "# initialize weights randomly with mean 0\n",
    "syn0 = 2*np.random.random((3,1)) - 1\n",
    "\n",
    "for iter in range(10000):\n",
    "    # forward propagation\n",
    "    l0 = X\n",
    "    l1 = nonlin(np.dot(l0,syn0))\n",
    " \n",
    "    # how much did we miss?\n",
    "    l1_error = y - l1\n",
    " \n",
    "    # multiply how much we missed by the \n",
    "    # slope of the sigmoid at the values in l1\n",
    "    l1_delta = l1_error * nonlin(l1,True)\n",
    " \n",
    "    # update weights\n",
    "    syn0 += np.dot(l0.T,l1_delta)\n",
    "print(\"Output After Training:\")\n",
    "print(l0)\n",
    "print(syn0)\n",
    "X2 = np.array([  [0,1,0],\n",
    "                 [1,1,0]])\n",
    "print(nonlin(np.dot(X2,syn0)))\n",
    "print(l1_error)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
